package yxy.game.pm2.command.tower;

import yxy.cherry.data.DataCenter;
import yxy.cherry.data.bean.packagedata;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.currency.Currencies;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.TowerMessage;
import yxy.game.pm2.module.RankWorshipHandle;
import yxy.game.pm2.module.tip.TipType;
import yxy.game.pm2.module.tip.checker.Checker_排行榜;
import yxy.game.pm2.module.tower.RankTower;

/** 修炼塔 修炼塔排行榜膜拜 */
@MessageType(proto = Command.TowerRankWorship, classtype = TowerMessage.TowerRankWorshipRequest.class)
public class CMD_TowerRankWorship extends Command<TowerMessage.TowerRankWorshipRequest> {

	@Override
	public void execute(MessageSource messageSource, TowerMessage.TowerRankWorshipRequest request) {
		String playerCode = messageSource.getTag();
		int ranking = request.getRank();

		TowerMessage.TowerRankWorshipResponse.Builder response = TowerMessage.TowerRankWorshipResponse.newBuilder();

		RankTower rank = RankTower.getRank(playerCode);
		RankWorshipHandle.RankWorshipEntry worshipEntry = new RankWorshipHandle.RankWorshipEntry();

		packagedata data = DataCenter.getData(50, packagedata.class);
		Currencies rewards = new Currencies();
		rewards.add(Currency.create(data.getRewardtype(), data.getRewardid(), data.getRewardnum()));

		Result result = new RankWorshipHandle().worship(playerCode, ranking, rank, rewards, worshipEntry);
		if (!result.equals(Result.Success)) {
			send(messageSource, response, result);
			return;
		}
		response.setRank(ranking);
		response.setFans(worshipEntry.getFans());
		response.addAllRewards(worshipEntry.getRewards());
		response.setTimes(worshipEntry.getTimes());

		send(messageSource, response, Result.Success);

		new Checker_排行榜().check(playerCode, rank, TipType.征战_排行榜按钮_修炼塔_膜拜);
	}

	private void send(MessageSource messageSource, TowerMessage.TowerRankWorshipResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}
}